﻿/*
 * Created by SharpDevelop.
 * User: Andrew Sadler
 * Date: 06/09/2012
 * Time: 22:27
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Globalization;
using System.IO;
using System.Threading;

using RRRSRoguelikeData;

namespace RRRSRoguelike.Helpers
{
	/// <summary>
	/// Description of Language.
	/// </summary>
	public class Language
	{
		//Possibly belongs in a helper class
		public static void NewLanguage(string culture)
		{
			//Set Properties
			Properties.Settings.Default.Culture = culture;
			//Set Culture
			Thread.CurrentThread.CurrentUICulture =
				CultureInfo.GetCultureInfo(Properties.Settings.Default.Culture);
			//Set Custom Folder path
			Properties.Settings.Default.CustomFolder =
				Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
				             "RRRSRoguelike", "CustomContent", Properties.Settings.Default.Culture);

			Properties.Settings.Default.Save();
			
			
			
			PrepareContentFolder();

		}
		
		//HACK: does this really belong in the language class?
		public static void BackupContentFolder()
		{
			//find path
			string sourcePath = SetTargetContentPath();
			string targetPath = sourcePath + "Backup";
			
			int i = 0;
			while(Directory.Exists(targetPath))
			{
				if(!Directory.Exists(targetPath+i.ToString()))
				   targetPath += i.ToString();
				else
				   i++;
				}
			
			try
            {
                Directory.Move(sourcePath, targetPath);  
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
			
			
		}

		public static void PrepareContentFolder()
		{
			string baseDir = AppDomain.CurrentDomain.BaseDirectory;

			var sourcePath = Path.Combine(baseDir,
			                              "CustomContent",
			                              Properties.Settings.Default.Culture);

			if (!Directory.Exists(sourcePath))
			{
				sourcePath = Path.Combine(baseDir,
				                          "CustomContent", "en-US");
			}

			string targetPath = SetTargetContentPath();

			//FIXME: add an option to backup and clear the custom folder.
			//Copy content files into custom folder.
			Files.CopyFolder(sourcePath, targetPath, false);
		}

		// -1 means the custom folder has not been set yet.
		//this will use a specified destination
		static string SetTargetContentPath()
		{
			string targetPath;
			if (Properties.Settings.Default.CustomFolder == "-1") 
			{
				targetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "RRRSRoguelike", "CustomContent");
				Properties.Settings.Default.CustomFolder = targetPath;
			} 
			else 
			{
				targetPath = Properties.Settings.Default.CustomFolder;
			}
			return targetPath;
		}
	}
}
